package com.itheima.guava.samples.demo.CollectionsDemo;

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.*;
import com.itheima.guava.samples.demo.SuppliersDemo.Girl;

import java.util.List;
import java.util.Map;

public class FluentIterableTest {

    private static List<Girl> girls;

    private static void setUp(){
        Girl girl1 = new Girl(17, "nice");
        Girl girl2 = new Girl(18, "not so nice");
        Girl girl3 = new Girl(19, "nice");
        Girl girl4 = new Girl(20, "not so nice");

        girls = Lists.newArrayList(girl1, girl2, girl3, girl4);
    }

    public static void testFluentIterable() {
        /**
         * from方法：用于包装Iterable接口，返回FluentIterable实例
         * filter方法：用于过滤集合中元素，返回过滤后的集合
         */
        FluentIterable<Girl> iterable = FluentIterable.from(girls).filter(new Predicate<Girl>() {
            @Override
            public boolean apply(Girl input) {
                return "nice".equals(input.getFace());
            }
        });

        for (Girl girl:iterable) {
            System.out.println(girl.getAge());
        }

        /**
         * transform方法：用于根据指定Function转换集合元素，返回转换后的集合
         */

        FluentIterable<String> fluentIterable = FluentIterable.from(girls).transform(new Function<Girl, String>() {
            @Override
            public String apply(Girl input) {
                return Joiner.on(",").join(input.getAge(), input.getFace());
            }
        });

        for (String girl:fluentIterable) {
            System.out.println(girl);
        }

        //isEmpty方法：是否为空
        boolean b = FluentIterable.from(girls).isEmpty();
        System.out.println(b);

        int size = FluentIterable.from(girls).size();
        System.out.println(size);

        Optional<Girl> first = FluentIterable.from(girls).first();
        System.out.println(first.get().toString());

        //转为List
        ImmutableList<Girl> girls = FluentIterable.from(FluentIterableTest.girls).toList();
        System.out.println(girls);

        //toMap
        ImmutableMap<Girl, String> map = FluentIterable.from(girls).toMap(new Function<Girl, String>() {
            @Override
            public String apply(Girl input) {
                return input.getAge()+","+input.getFace();
            }
        });

        ImmutableSet<Map.Entry<Girl, String>> entries = map.entrySet();

        for (Map.Entry<Girl, String> entry:map.entrySet()) {
            Girl key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key + ":" + value);
        }

        ImmutableSet<Girl> set = FluentIterable.from(girls).toSet();
        System.out.println(set);


        ImmutableList<String> immutableList = FluentIterable.from(girls).transform(new Function<Girl, String>() {
            @Override
            public String apply(Girl input) {
                return Joiner.on(",").join(input.getAge(), input.getFace());
            }
        }).limit(1).toList();

        System.out.println(immutableList);
    }



    public static void main(String[] args) {

        setUp();
        testFluentIterable();

    }

}
